const express = require('express')

const app = express()

app.use(express.urlencoded({ extended: true }))

app.listen(8080, () => {
    console.log('服务器已启动')
})

// 开放静态资源权限
app.use('/uploads', express.static('./uploads'))
    //通过http://localhost:8080/uploads/XXXXXX访问

//身份验证
const jwt = require('express-jwt')
app.use(jwt({
    secret: 'haha',
    algorithms: ['HS256']
}).unless({
    path: ['/api/v1/admin/user/login']
}))



//文章类别相关接口
const category = require('./router/category')
app.use('/api/v1', category)



//用户相关接口
const user = require('./router/user')
app.use('/api/v1', user)


//文章相关接口
const airticle = require('./router/article')
app.use('/api/v1', airticle)



//身份验证错误处理
app.use((err, req, res, next) => {
    if (err.name === 'UnauthorizedError') {
        res.status(403).send({ code: 403, message: '身份认证失败！' });
    }
})